package com.xemxoso.vanthan.mientrung;

import java.util.ArrayList;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xemxoso.R;
import com.xemxoso.Variables;
import com.xemxoso.jsouplocal.JsoupProvince;
import com.xemxoso.thuhien.miennam.ItemListProvince;

public class FragmentMienTrung extends Fragment {

	ProgressDialog mProgressDialog;
	// int id = 0;
	TextView txttitle;

	ItemListProvinceAdapter madapter;
	ListView list;
	View rootView;

	public FragmentMienTrung() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_mien_trung,
				container, false);

		return rootView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		txttitle = (TextView) view.findViewById(R.id.text_title_truyen_thong);
		list = (ListView) view.findViewById(R.id.listView_mien_trung);
		madapter = new ItemListProvinceAdapter(getActivity(),
				R.layout.item_list_tinh_custome,
				new ArrayList<ItemListProvinceEntity>());

		list.setAdapter(madapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

				Fragment fragment = new ItemListProvince();
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					fragmentManager.beginTransaction()
							.replace(R.id.container, fragment).commit();
				}
			}
		});
		JsoupMTrung jMienTrung = new JsoupMTrung();
		jMienTrung.execute(Variables.site);
		super.onViewCreated(view, savedInstanceState);
	}

	public class JsoupMTrung extends AsyncTask<String, Void, Void> {
		private ArrayList<ItemListProvinceEntity> mItemProvince;
		private JsoupProvince jProvince;
		@Override
		protected void onPreExecute() {
			jProvince = new JsoupProvince();
			mItemProvince = new ArrayList<ItemListProvinceEntity>();
			mProgressDialog = new ProgressDialog(getActivity());
			mProgressDialog.setTitle("Please waiting ...");
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.show();
		}
		
		@Override
		protected Void doInBackground(String... params) {

			mItemProvince=jProvince.parseJsoupProvince(params[0],2);
			
			return null;

		}

		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Void result) {

			madapter.addAll(mItemProvince);
			madapter.notifyDataSetChanged();
			mProgressDialog.dismiss();
			super.onPostExecute(result);
		}

	}

}
